####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A1
###
####################################################################################

library(dplyr)
library(miceadds)
library(AER)
library(rlist)
library(ggplot2)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################
# Auxiliary Functions
################################################################################

makeplot = function(model_output, hete_var){
  groups = c(paste0('Low ', hete_var), paste0('Medium ', hete_var), paste0('High ', hete_var))
  container = lapply(model_output, function(x) rbind(cbind(x[2:6,], period = c(-4, -3, -2, 1, 3)), c(0,0,NA,NA,-1), c(NA,NA,NA,NA,0),  c(NA,NA,NA,NA,2)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(groups, each=8))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = groups)
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f, shape = group.f))+
    geom_point(position = position_dodge(width = 0.5), size=3) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), linewidth=1, width=.15, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Period relative to policy implementation") + 
    ylab("NF Vote Share (percentage points)") +
    theme_light()+
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}


################################################################################
# Immigration in 1990
################################################################################

modelli = list()
for (n in c(1,2,3)){
 
  print(paste0('Immigation tercile ', n, ' -------------- '))
  
  df1 = df_did[which(!is.na(df_did$fn) & df_did$imm_quant_90==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + 
              factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  
  modelli[[n]] = temp
}

makeplot(modelli,  'Immigration 1990')
ggsave(paste0(wd_res, '/figures/figA1a.pdf'), height=7, width=10)



################################################################################
# Immigration change between in 1990 and 1999
################################################################################

modelli = list()
for (n in c(1,2,3)){
  
  print(paste0('Immigation tercile ', n, ' -------------- '))
  
  df1 = df_did[which(!is.na(df_did$fn) & df_did$delta_imm_quant==n),]
  
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + 
              factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  
  modelli[[n]] = temp
}


makeplot(modelli,  'Immigration 1999-1990')

ggsave(paste0(wd_res, '/figures/figA1b.pdf'), height=7, width=10)


